package com.wuster.config;


import com.wuster.interceptor.StudentAuthInterceptor;
import com.wuster.interceptor.TeacherAuthInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    StudentAuthInterceptor studentAndUserAuthInterceptor;
    @Autowired
    TeacherAuthInterceptor teacherAuthInterceptor;

    /**
     * 注册自定义拦截器
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry){
        log.info("开始注册自定义拦截器...");
        // 给除了 /login 和 /add 的接口都配置拦截器,拦截转向到 Interceptor

        //student user拦截器
        registry.addInterceptor(studentAndUserAuthInterceptor)
                .addPathPatterns("/student/**")
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/add")
                .excludePathPatterns("/user/hello");

        //teacher拦截器
        registry.addInterceptor(teacherAuthInterceptor)
                .addPathPatterns("/teacher/**");
    }

    /**
     * 扩展SpringMVC的消息转换器
     * @param converters
     */
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("拓展消息转换器");
        // 创建一个消息转换器对象
        // 需要为消息转化器设置一个对象转换器，对象转换器可以将java对象序列化为json数据
        // 将自己的消息转换器加入容器中
    }
}
